package com.zeta.eduservice.controller.question;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeta.commonutils.R;
import com.zeta.eduservice.entity.and.EduClassAndPublichomework;
import com.zeta.eduservice.entity.and.EduCourseAndPublichomework;
import com.zeta.eduservice.entity.and.EduFolderAndPublichomework;
import com.zeta.eduservice.entity.question.EduPublishhomework;
import com.zeta.eduservice.entity.vo.CourseQuery;
import com.zeta.eduservice.entity.vo.PublishQuery;
import com.zeta.eduservice.entity.vo.front.PublishVo;
import com.zeta.eduservice.mapper.EduClassAndPublichomeworkMapper;
import com.zeta.eduservice.mapper.EduFolderAndPublichomeworkMapper;
import com.zeta.eduservice.service.EduClassAndPublichomeworkService;
import com.zeta.eduservice.service.EduCourseAndPublichomeworkService;
import com.zeta.eduservice.service.EduFolderAndPublichomeworkService;
import com.zeta.eduservice.service.EduPublishhomeworkService;

import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 发布作业 前端控制器
 * </p>
 *
 * @author zeta
 * @since 2020-11-10
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu_publishhomework")
public class EduPublishhomeworkController {

	@Autowired
	EduFolderAndPublichomeworkService folderAndPublichomeworkService;
	@Autowired
	EduCourseAndPublichomeworkService courseAndPublichomeworkService;
	@Autowired
	EduClassAndPublichomeworkService classAndPublichomeworkService;
	@Autowired
	EduPublishhomeworkService publishHomeworkService;
	@PostMapping("")
	public R add(@RequestBody String s) {
		JSONObject parseObject = JSONObject.parseObject(s);
		EduPublishhomework eduPublishhomework = new EduPublishhomework();
		eduPublishhomework = parseObject.getObject("publicHomework", EduPublishhomework.class);
		publishHomeworkService.save(eduPublishhomework);
		List<String> folerList = parseObject.getJSONArray("folderList").toJavaList(String.class);
		List<String> classList = parseObject.getJSONArray("classList").toJavaList(String.class);
		List<String> courseList = parseObject.getJSONArray("courseList").toJavaList(String.class);
		publishHomeworkService.add(folerList,classList,courseList,eduPublishhomework);
		return R.ok();
	}
	@DeleteMapping("{id}")
	public R delete(@PathVariable String id) {
		
	publishHomeworkService.removeById(id);
	QueryWrapper<EduFolderAndPublichomework> queryWrapper1=new 	QueryWrapper<EduFolderAndPublichomework>();
	queryWrapper1.eq("publishomework_id", id);
	QueryWrapper<EduClassAndPublichomework> queryWrapper2=new 	QueryWrapper<EduClassAndPublichomework>();
	queryWrapper2.eq("publishomework_id", id);
	QueryWrapper<EduCourseAndPublichomework> queryWrapper3=new 	QueryWrapper<EduCourseAndPublichomework>();
	queryWrapper2.eq("publishomework_id", id);
	folderAndPublichomeworkService.remove(queryWrapper1);
	classAndPublichomeworkService.remove(queryWrapper2);
	courseAndPublichomeworkService.remove(queryWrapper3);
		return R.ok();
	}
	@GetMapping("{teacherId}")
	public R get(@PathVariable String teacherId) {
		QueryWrapper<EduPublishhomework> queryWrapper =new  QueryWrapper<EduPublishhomework>();
		queryWrapper.eq("teacher_id",teacherId);
		List<EduPublishhomework> list = publishHomeworkService.list(queryWrapper);
		return R.ok().data("publishlist",list);
	}
	@GetMapping("getPublishInfo/{id}")
	public R getPublish(@PathVariable String id) {
		PublishVo list = publishHomeworkService.getList(id);
		return R.ok().data("courseList",list.getByPublishId2()).data("classList", list.getByPublishId3()).data("folderList",list.getByPublishId());
	}
	@PostMapping("{page}/{limit}")
	public R pageQuery(
			@ApiParam(name = "page", value = "当前页码", required = true)
			@PathVariable Long page,
			@ApiParam(name = "limit", value = "每页记录数", required = true)
			@PathVariable Long limit,
			@ApiParam(name = "searchObj", value = "查询对象", required = false)
			@RequestBody PublishQuery searchObj){
	
		Page<EduPublishhomework> pageParam = new Page<>(page, limit);
		List<EduPublishhomework> pageByClassId = publishHomeworkService.getPageByClassId(pageParam,searchObj);
		return R.ok().data("publishList",pageByClassId).data("total", pageParam.getTotal());
	}
}

